package com.aiyige.utils.widget;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;

public class ActivityDescriptionView extends FrameLayout {
    public static final String TAG = ActivityDescriptionView.class.getSimpleName();
    public static final int COLLAPSE_MAX_LINES = 2;
    TextView descTitleTv;
    TextView descContentTv;
    FrameLayout activityDescActionLayout;
    LinearLayout activityDescExpandLayout;
    LinearLayout activityDescCollapseLayout;
    boolean collapse = true;
    boolean handleExpandCollapseFinish = true;

    public ActivityDescriptionView(@NonNull Context context) {
        super(context);
        init();
    }

    public ActivityDescriptionView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ActivityDescriptionView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.activity_description_view, this, true);
        descTitleTv = findViewById(R.id.descTitleTv);
        descContentTv = findViewById(R.id.descContentTv);
        activityDescActionLayout = findViewById(R.id.activityDescActionLayout);
        activityDescExpandLayout = findViewById(R.id.activityDescExpandLayout);
        activityDescCollapseLayout = findViewById(R.id.activityDescCollapseLayout);
        activityDescCollapseLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                collapse();
            }
        });
        activityDescExpandLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                expand();
            }
        });
        setVisibility(GONE);
    }

    public void expand() {
        handleExpandCollapseFinish = false;
        collapse = false;
        requestLayout();
    }

    public void collapse() {
        handleExpandCollapseFinish = false;
        collapse = true;
        requestLayout();
    }

    public void setTitle(String title) {
        descTitleTv.setText(Html.fromHtml(title));
    }

    public void setContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            setVisibility(VISIBLE);
            descContentTv.setText(content/*Html.fromHtml(content)*/);
            handleExpandCollapseFinish = false;
            requestLayout();
        } else {
            setVisibility(GONE);
        }
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (handleExpandCollapseFinish) return;
        if (collapse) {
            if (descContentTv.getLineCount() > COLLAPSE_MAX_LINES) {
                descContentTv.setMaxLines(COLLAPSE_MAX_LINES);
                showExpandLayout();
                requestLayout();
            } else {
                descContentTv.setMaxLines(Integer.MAX_VALUE);
                hideActionLayout();
                requestLayout();
            }
        } else {
            descContentTv.setMaxLines(Integer.MAX_VALUE);
            showCollapseLayout();
            requestLayout();
        }
        handleExpandCollapseFinish = true;
    }

    private void showExpandLayout() {
        activityDescActionLayout.setVisibility(VISIBLE);
        activityDescExpandLayout.setVisibility(VISIBLE);
        activityDescCollapseLayout.setVisibility(GONE);
    }

    private void showCollapseLayout() {
        activityDescActionLayout.setVisibility(VISIBLE);
        activityDescExpandLayout.setVisibility(GONE);
        activityDescCollapseLayout.setVisibility(VISIBLE);
    }

    private void hideActionLayout() {
        activityDescActionLayout.setVisibility(GONE);
    }


    public int dp2px(float dipValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dipValue * scale);
    }

}
